package haw.pr2.A2a;

import java.text.DecimalFormat;
import java.util.Set;
import jgame.JGColor;
import jgame.JGFont;
import jgame.JGPoint;
import jgame.platform.JGEngine;

/**
 *
 * @author Marvin
 */
class Engine extends JGEngine {

    /**
     * A timer used to animate the "hello world" text.
     */
    private double lastFrameAtInMillis = System.currentTimeMillis();
    private double level = 0.0;
    private double TIME_TO_FULL_LEVEL = 400; //in  ms
    private Set particles;

    public Engine(JGPoint size) {
        // This inits the engine as an application.
        initEngine(size.x, size.y);
    }

    public void setParticles(Set particles) {
        this.particles = particles;
    }

    public void initCanvas() {
        setCanvasSettings(
                20, // width of the canvas in tiles
                15, // height of the canvas in tiles
                16, // width of one tile
                16, // height of one tile
                //    (note: total size = 20*16=320  x  15*16=240)
                null,// foreground colour -> use default colour white
                new JGColor(0, 0, 0),// background colour -> use default colour black
                null // standard font -> use default font
                );
    }

    @Override
    public void initGame() {
        setFrameRate(
                35,// 35 = frame rate, 35 frames per second
                2 //  2 = frame skip, skip at most 2 frames before displaying
                //      a frame again
                );
        lastFrameAtInMillis = System.currentTimeMillis();

        //Bilder definieren
        defineImage("porsche1", "-", 0, "porsche.png", "-");
        defineImage("baem", "-", 0, "Explosion.gif", "-");
    }

    /**
     * Game logic is done here. No painting can be done here, define paintFrame
     * to do that.
     */
    @Override
    public void doFrame() {
        final double FBREAK = -1.0;
        final double FLEVEL = 1.0;
        final double NEUTRAL = 0.0;
        long currentMillis = System.currentTimeMillis();
        for (Object value : particles) {
            ParticleInterface particle = (ParticleInterface) value;
            double TimeInMilis = (double) (currentMillis - lastFrameAtInMillis);

            


            /* Abfangen der Eingabe
             * Enter -> ABS ein bzw. Aus
             * Backspace -> ASR ein bzw. Aus
             */
            if (getKey(KeyEsc)) {
                    particle.restGame();
                    clearKey(KeyEsc);
                }
            
            if (!particle.getExplState()) {

                if (getKey(KeyEnter)) {
                    particle.toggleAbs();
                    clearKey(KeyEnter);
                } else if (getKey(KeyBackspace)) {
                    particle.toggleAsr();
                    clearKey(KeyBackspace);
                }

                
                
                if (getKey(KeyCtrl)) {
                    particle.toggleUnderground();
                    clearKey(KeyCtrl);
                }


                /* Beschleunigung bzw. Bremsen
                 * Realisierung der Beschleunigung bzw. Bremse als "Taster".
                 * Verzoegerung um TIME_TO_FULL_LEVEL
                 */
                if (getKey(KeyDown)) {
                    double newLevel = 1.0 / TIME_TO_FULL_LEVEL * TimeInMilis;
                    newLevel = level - newLevel;
                    level = newLevel < FBREAK ? FBREAK : newLevel;
                    
                } else if (getKey(KeyUp)) {
                    double newLevel = 1.0 / TIME_TO_FULL_LEVEL * TimeInMilis;
                    newLevel = level + newLevel;
                    level = newLevel > FLEVEL ? FLEVEL : newLevel;
                } else {
                    // Wenn beschleunigt wurde, dann veringert sich die Kraft 
                    // von +1 bis 0
                    if (level >= 0) {
                        double newLevel = 1.0 / TIME_TO_FULL_LEVEL * TimeInMilis;
                        newLevel = level - newLevel;
                        level = newLevel < NEUTRAL ? NEUTRAL : newLevel;
                    // Wenn gebremst wurde, dann veringert sich die Bremstkraft
                    // von -1 bis 0
                    } else {
                        double newLevel = 1.0 / TIME_TO_FULL_LEVEL * TimeInMilis;
                        newLevel = level + newLevel;
                        level = newLevel;
                    }
                }

            }



            //Do Simulation Step
            particle.step(TimeInMilis / 1000, level);
        }
        lastFrameAtInMillis = System.currentTimeMillis();
    }

    /**
     * Any graphics drawing beside objects or tiles should be done here.
     * Usually, only status / HUD information needs to be drawn here.
     */
    @Override
    public void paintFrame() {
        DecimalFormat f = new DecimalFormat("#0.00");

        setColor(JGColor.black);
        for (Object value : particles) {
            ParticleInterface particle = (ParticleInterface) value;
            // drawOval(particle.getXInMeters(), particle.getYInMeters(), 5, 2.5, true, true);
            drawString(f.format(particle.getSpeed()) + "km/h  " + "Time: " + f.format(particle.getTime()) + "s Level: " + f.format(Math.round(level * 100d) / 100d) + " ABS: " + particle.getAbsState() + " ASR: " + particle.getAsrState() + " Underground: " + particle.getUnderground(),
                    10, // xpos
                    10, // ypos
                    -1, // the text alignment
                    null,
                    JGColor.white);
            if (particle.getExplState()) {
                drawImage(particle.getXInMeters(), particle.getYInMeters(), "baem");
            } else {
                drawImage(particle.getXInMeters(), particle.getYInMeters(), "porsche1");
            }
        }
    }
}